﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sgr.Email
{
    public class SmtpInfo
    {
        private SmtpInfo()
        { }

        /// <summary>
        /// 从邮件配置创建SMTP信息
        /// </summary>
        /// <param name="options">邮件配置选项</param>
        /// <returns>SMTP信息</returns>
        /// <exception cref="ArgumentNullException">配置为空时抛出</exception>
        /// <exception cref="ArgumentException">必要的配置项缺失时抛出</exception>
        public static SmtpInfo Create(EmailOptions options)
        {
            Check.NotNull(options, "options");

            // 验证必要的配置项
            if (string.IsNullOrEmpty(options.Host))
                throw new ArgumentException("SMTP服务器地址不能为空", nameof(options));

            if (options.Port <= 0 || options.Port > 65535)
                throw new ArgumentException("无效的SMTP服务器端口", nameof(options));

            if (string.IsNullOrEmpty(options.UserName) && options.AuthenticationType != SmtpAuthenticationType.Anonymous)
                throw new ArgumentException("SMTP用户名不能为空", nameof(options));

            if (string.IsNullOrEmpty(options.Password) && options.AuthenticationType != SmtpAuthenticationType.Anonymous)
                throw new ArgumentException("SMTP密码不能为空", nameof(options));

            if (string.IsNullOrEmpty(options.DefaultFromAddress))
                throw new ArgumentException("默认发件人地址不能为空", nameof(options));

            // 创建实例并设置属性
            var info = new SmtpInfo
            {
                // SMTP服务器配置
                Host = options.Host,
                Port = options.Port,
                TimeoutSeconds = options.TimeoutSeconds,
                VerificationType = options.VerificationType,
                ValidateServerCertificate = options.ValidateServerCertificate,

                // 认证信息
                UserName = options.UserName,
                Password = options.Password,
                AuthenticationType = options.AuthenticationType,

                // 发送配置
                From = options.DefaultFromAddress,
                Sender = options.DefaultFromDisplayName
            };

            return info;
        }

        /// <summary>
        /// 创建基本认证的SMTP信息
        /// </summary>
        /// <param name="host">SMTP服务器地址</param>
        /// <param name="port">SMTP服务器端口</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="fromAddress">发件人地址</param>
        /// <param name="fromDisplayName">发件人显示名称</param>
        /// <param name="verificationType">验证类型（默认TLS）</param>
        /// <returns>SMTP信息</returns>
        public static SmtpInfo CreateBasic(
            string host,
            int port,
            string userName,
            string password,
            string fromAddress,
            string? fromDisplayName = null,
            string verificationType = "TLS")
        {
            // 验证参数
            if (string.IsNullOrEmpty(host))
                throw new ArgumentException("SMTP服务器地址不能为空", nameof(host));

            if (port <= 0 || port > 65535)
                throw new ArgumentException("无效的SMTP服务器端口", nameof(port));

            if (string.IsNullOrEmpty(userName))
                throw new ArgumentException("用户名不能为空", nameof(userName));

            if (string.IsNullOrEmpty(password))
                throw new ArgumentException("密码不能为空", nameof(password));

            if (string.IsNullOrEmpty(fromAddress))
                throw new ArgumentException("发件人地址不能为空", nameof(fromAddress));

            // 创建实例
            return new SmtpInfo
            {
                Host = host,
                Port = port,
                UserName = userName,
                Password = password,
                From = fromAddress,
                Sender = fromDisplayName ?? fromAddress,
                VerificationType = verificationType,
                AuthenticationType = SmtpAuthenticationType.Basic
            };
        }

        /// <summary>
        /// 创建授权码认证的SMTP信息
        /// </summary>
        /// <param name="host">SMTP服务器地址</param>
        /// <param name="port">SMTP服务器端口</param>
        /// <param name="userName">用户名</param>
        /// <param name="authCode">授权码</param>
        /// <param name="fromAddress">发件人地址</param>
        /// <param name="fromDisplayName">发件人显示名称</param>
        /// <param name="verificationType">验证类型（默认TLS）</param>
        /// <returns>SMTP信息</returns>
        public static SmtpInfo CreateWithAuthCode(
            string host,
            int port,
            string userName,
            string authCode,
            string fromAddress,
            string? fromDisplayName = null,
            string verificationType = "TLS")
        {
            // 验证参数
            if (string.IsNullOrEmpty(host))
                throw new ArgumentException("SMTP服务器地址不能为空", nameof(host));

            if (port <= 0 || port > 65535)
                throw new ArgumentException("无效的SMTP服务器端口", nameof(port));

            if (string.IsNullOrEmpty(userName))
                throw new ArgumentException("用户名不能为空", nameof(userName));

            if (string.IsNullOrEmpty(authCode))
                throw new ArgumentException("授权码不能为空", nameof(authCode));

            if (string.IsNullOrEmpty(fromAddress))
                throw new ArgumentException("发件人地址不能为空", nameof(fromAddress));

            // 创建实例
            return new SmtpInfo
            {
                Host = host,
                Port = port,
                UserName = userName,
                Password = authCode,
                From = fromAddress,
                Sender = fromDisplayName ?? fromAddress,
                VerificationType = verificationType,
                AuthenticationType = SmtpAuthenticationType.AuthCode
            };
        }

        #region SMTP服务器配置

        /// <summary>
        /// SMTP服务器地址
        /// </summary>
        public string Host { get; private set; } = string.Empty;

        /// <summary>
        /// SMTP服务器端口
        /// </summary>
        public int Port { get; private set; }

        /// <summary>
        /// 连接超时时间（秒）
        /// </summary>
        public int TimeoutSeconds { get; private set; } = 30;

        /// <summary>
        /// 验证类型
        /// <para>None: 无验证</para>
        /// <para>StartTls: 使用 STARTTLS 加密</para>
        /// <para>SslOnConnect: 使用 SSL/TLS 加密</para>
        /// </summary>
        public string VerificationType { get; private set; } = "TLS";

        /// <summary>
        /// 是否验证服务器证书
        /// </summary>
        public bool ValidateServerCertificate { get; private set; } = false;

        #endregion SMTP服务器配置

        #region 认证信息

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; private set; } = string.Empty;

        /// <summary>
        /// 密码/授权码
        /// </summary>
        public string Password { get; private set; } = string.Empty;

        /// <summary>
        /// 认证类型
        /// </summary>
        public SmtpAuthenticationType AuthenticationType { get; private set; } = SmtpAuthenticationType.Basic;

        #endregion 认证信息

        #region 发送配置

        /// <summary>
        /// 发件人地址
        /// </summary>
        public string From { get; private set; } = string.Empty;

        /// <summary>
        /// 发件人显示名称
        /// </summary>
        public string Sender { get; private set; } = string.Empty;

        #endregion 发送配置
    }
}